package com.aac.rabbitrfc.manager;

import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.ext.DestinationDataProvider;
import org.json.JSONObject;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;

/**
 * 连接SAP
 */
public class RfcManager {

	private static final String ABAP_AS = "ABAP_AS_WITHOUT_POOL";

	private JCoDestination destination;

	private RfcManager(JSONObject SAPConfig) throws Exception {
		connect(SAPConfig);
	}

	public static RfcManager getInstance(JSONObject SAPConfig)
			throws Exception {
		RfcManager common = new RfcManager(SAPConfig);
		return common;
	}

	public void connect(JSONObject SAPConfig) throws Exception {
		String host = SAPConfig.get("ashost").toString();
		String clientName = SAPConfig.get("client").toString();
		String language = SAPConfig.get("langu").toString();
		String userId = SAPConfig.get("user").toString();
		String password = SAPConfig.get("passwd").toString();
		String system = SAPConfig.get("sysnr").toString();
		String JCO_PEAK_LIMIT = SAPConfig.get("Description").toString();
		Properties connectProperties = new Properties();
		connectProperties.clear(); 
		connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, host);
		connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, system);
		connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, clientName);
		connectProperties.setProperty(DestinationDataProvider.JCO_USER, userId);
		connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, password);
		connectProperties.setProperty(DestinationDataProvider.JCO_LANG, language);
		connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, JCO_PEAK_LIMIT);
		connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "30");
		try {
			createDataFile(ABAP_AS, "jcoDestination", connectProperties);
			destination = JCoDestinationManager.getDestination(ABAP_AS);
		} catch (JCoException ex) {
			throw new Exception("SAP连接失败" + ex.getMessage());
		}
	}

	public JCoDestination getDestination(){
		try {
			destination = JCoDestinationManager.getDestination(ABAP_AS);
		} catch (JCoException e) {
			e.printStackTrace();
		}
		return destination;
	}

	public static void createDataFile(String name, String suffix, Properties properties)
			throws Exception {
		// Windows
		File cfg = new File(name + "." + suffix);
		// Linux
//		File cfg = new File("/opt/" + name + "." + suffix);
		try {
			FileOutputStream fos = new FileOutputStream(cfg, false);
			properties.store(fos, "ABAP_AS_WITHOUT_POOL");
			fos.close();
		} catch (Exception e) {
			throw new Exception("不能创建SAP连接需要的Destination文件" + cfg.getName());
		}
	}

	public JCoFunction getFunction(JCoDestination destination, String functionName) {
		JCoFunction function = null;
		 try {
			function = destination.getRepository().getFunctionTemplate(functionName).getFunction();
		} catch (JCoException e) {
			e.printStackTrace();
		}  
		return function;
	}

}